package com.imooc.queue;

import com.imooc.array.Array;

/**
 * 基于数组实现的队列
 * 一向入队，一向出队
 * 先进先出
 */
public class ArrayQueue<T> implements Queue<T> {

    private Array<T> array;

    public ArrayQueue() {
        array = new Array<>();
    }

    public ArrayQueue(int capacity) {
        array = new Array<>(capacity);
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void enqueue(T e) {
        array.addLast(e);
    }

    /**
     * 时间复杂度o(n)，因为每次出队，后边元素要一次向前挪
     * @return
     */
    @Override
    public T dequeue() {
        return array.removeFirst();
    }

    @Override
    public T getFront() {
        return array.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("ArrayQueue : size = %d, capacity = %d\n", array.getSize(), array.getCapacity()));
        sb.append("front [");
        for(int i = 0; i < array.getSize(); i++) {
            sb.append(array.get(i));
            if(i != array.getSize() - 1)
                sb.append(", ");
        }
        sb.append(" ] end");
        return sb.toString();
    }

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue();
        for(int i = 0; i < 10; i++) {
            arrayQueue.enqueue(i);
        }
        System.out.println(arrayQueue);

        arrayQueue.dequeue();
        System.out.println(arrayQueue);
    }
}
